<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

class Wms_model extends CI_Model
{
    /**
     * 释放订单锁定库存
     * @param  string|array $orderIds 订单id order id
     * @date   2019/01/12
     * @author longli
     */
    public function orderReleaseStock($orderIds)
    {
        if(is_array($orderIds))
            $orderIds = implode(',', $orderIds);
        $orderSql = "select id, billNo from " . $this->db->dbprefix('order') . " where id in($orderIds)";
        $rowsOrder = $this->mysql_model->query($orderSql, 2);
        if(empty($rowsOrder)) return;
        $orderNos = array_column($rowsOrder, 'billNo');
        $db = $this->db;
        $db->select("id");
        $db->from($this->db->dbprefix("invoice"));
        $db->where(['transType' => 150811, 'isDelete' => 0]);
        $db->where_in('srcOrderId', array_column($rowsOrder, 'id'));
        $db->where_in('srcOrderNo', $orderNos);
        $iid = $db->get()->result_array();
        if(empty($iid)) return;
        $iid = implode(',', array_column($iid, 'id'));
        $this->db->trans_begin();
        $this->mysql_model->delete('invoice', "id IN($iid)");
        $this->mysql_model->delete('invoice_info', "iid IN($iid)");
        if($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
        }
        else
        {
            $this->db->trans_commit();
            $this->common_model->logs('解锁库存：' . implode(', ', $orderNos));
        }
    }

    /**
     * 审核订单锁定库存
     * @param string|array $orderIds 订单id order id
     * @date   2019/01/09
     * @author longli
     */
    public function orderLockStock($orderIds)
    {
        if(is_array($orderIds))
            $orderIds = implode(',', $orderIds);
        $rows = $this->mysql_model->get_results('order', "id IN($orderIds) and isDelete = 0");
        $profit = $this->data_model->get_profit('and billDate<="' . date('Y-m-d') . '"');
        foreach($rows as $row)
        {
            $info = [
                'billNo', 'billType', 'srcOrderId', 'srcOrderNo', 'transType', 'transTypeName', 'buId', 'createTime',
                'billDate', 'description', 'totalQty', 'totalAmount', 'outLocationId',
                'uid', 'userName', 'postData'
            ];
            $orderData = [];
            $orderData['billType'] = 'OO';
            $orderData['transType'] = 150811;
            $orderData['transTypeName'] = '锁定库存';
            $orderData['createTime'] = date('Y-m-d H:i:s');
            $orderData['billDate'] = date('Y-m-d');
            $orderData['srcOrderNo'] = $row['billNo'];
            $orderData['srcOrderId'] = $row['id'];
            $orderData = array_merge($row, $orderData);
            if(empty($orderData['postData']))
                $orderData['postData'] = serialize($orderData);
            $info = elements($info, $orderData);
            $iid = $this->mysql_model->insert('invoice', $info);

            $amount = 0;
            $v = [];
            foreach($this->mysql_model->get_results("order_info", ['iid' => $row['id'], 'isDelete' => 0]) as $k => $info_row)
            {
                $price = isset($profit['inprice'][$info_row['invId']][$info_row['locationId']]) ? $profit['inprice'][$info_row['invId']][$info_row['locationId']] : 0;
                $amount += -abs($info_row['qty']) * $price;
                $v[$k]['iid'] = $iid;
                $v[$k]['billNo'] = $orderData['billNo'];
                $v[$k]['buId'] = $orderData['buId'];
                $v[$k]['transType'] = $orderData['transType'];
                $v[$k]['transTypeName'] = $orderData['transTypeName'];
                $v[$k]['billDate'] = $orderData['billDate'];
                $v[$k]['billType'] = $orderData['billType'];
                $v[$k]['invId'] = intval($info_row['invId']);
                $v[$k]['skuId'] = intval($info_row['skuId']);
                $v[$k]['unitId'] = intval($info_row['unitId']);
                $v[$k]['locationId'] = intval($info_row['locationId']);
                $v[$k]['qty'] = -abs($info_row['qty']);
                $v[$k]['amount'] = -abs($info_row['qty']) * $price;
                $v[$k]['price'] = $price;
                $v[$k]['description'] = $info_row['description'];
            }
            if(!empty($v))
            {
                $invoiceId = $this->mysql_model->get_row('invoice', [
                    'transType' => 150811,
                    'srcOrderNo' => $row['billNo'],
                    'srcOrderId' => $row['id'],
                ], 'id');
                if(!empty($invoiceId))
                {
                    $this->mysql_model->delete('invoice_info', ['iid' => $invoiceId]);
                }
                $this->mysql_model->insert('invoice_info', $v);
                $this->mysql_model->update('invoice', array('totalAmount' => $amount), array('id' => $iid));
            }
            $this->common_model->logs('锁定库存：' . $orderData['billNo']);

        }
    }

}